<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生学号" prop="stuCode">
        <el-input
          v-model="queryParams.stuCode"
          placeholder="请输入学生学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属班级" prop="studentClassId">
        <el-select v-model="queryParams.studentClassId" placeholder="所属班级" clearable style="width: 200px">
          <el-option
              v-for="dict in classList"
              :key="dict.id"
              :label="dict.className"
              :value="dict.id"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="年龄" prop="age">-->
<!--        <el-input-->
<!--          v-model="queryParams.age"-->
<!--          placeholder="请输入年龄"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出生时间" prop="brithdayTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.brithdayTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择出生时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="入学时间" prop="enterschoolTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.enterschoolTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择入学时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="结业时间" prop="graduateTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.graduateTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择结业时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['student:studentInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:studentInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:studentInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['student:studentInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="学生学号" align="center" prop="stuCode" />
      <el-table-column label="学生姓名" align="center" prop="stuName" />
      <el-table-column label="性别" align="center" prop="sex">
      <template #default="scope">
        <dict-tag :options="sys_user_sex" :value="scope.row.status" />
      </template>
      </el-table-column>
      <el-table-column label="所属班级" align="center" prop="studentClassId" >
        <template #default="scope">
          <span>{{ getClassNameById(scope.row.studentClassId) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="出生时间" align="center" prop="brithdayTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.brithdayTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="入学时间" align="center" prop="enterSchoolTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.enterSchoolTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结业时间" align="center" prop="graduateTime" width="180">
        <template #default="scope">
          <span >{{ scope.row.graduateTime == null?'----':parseTime(scope.row.graduateTime, '{y}-{m}-{d}') }} </span>
        </template>
      </el-table-column>
      <el-table-column label="学籍状态" align="center" key="status" >
        <template #default="scope">
          <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
              @change="handleStatusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['student:studentInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['student:studentInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" he append-to-body>
      <el-form ref="studentInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
        <el-col :span="12">
          <el-form-item label="学生学号" prop="stuCode">
            <el-input v-model="form.stuCode" placeholder="请输入学生学号" />
          </el-form-item>
        </el-col >
        <el-col :span="12">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form.stuName" placeholder="请输入学生姓名" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄" prop="age">
            <el-input v-model="form.age" placeholder="请输入年龄" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option v-for="dict in sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in student_status" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
        </el-col>
          <el-col :span="12">
            <el-form-item label="所属班级" prop="jobGroup">
              <el-select v-model="form.studentClassId" placeholder="请选择">
                <el-option
                    v-for="dict in classList"
                    :key="dict.id"
                    :label="dict.className"
                    :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        <el-col :span="12">
          <el-form-item label="出生时间" prop="brithdayTime">
            <el-date-picker clearable
                            v-model="form.brithdayTime"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="请选择出生时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="入学时间" prop="enterSchoolTime">
          <el-date-picker clearable
            v-model="form.enterSchoolTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择入学时间">
          </el-date-picker>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="结业时间" prop="graduateTime">
          <el-date-picker clearable
            v-model="form.graduateTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择结业时间">

          </el-date-picker>
        </el-form-item>
        </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="StudentInfo">
import {
  listStudentInfo,
  getStudentInfo,
  delStudentInfo,
  addStudentInfo,
  updateStudentInfo,
  changeStudentStatus,
} from "@/api/student/studentInfo";

import {
  getAllClassList
} from "@/api/student/class";

const { proxy } = getCurrentInstance();
const { student_status, sys_user_sex } = proxy.useDict("student_status", "sys_user_sex");

const studentInfoList = ref([]);
const classList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const names = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// const className = ref("大学一年级");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    stuCode: null,
    stuName: null,
    sex: null,
    age: null,
    brithdayTime: null,
    enterSchoolTime: null,
    graduateTime: null,
    status: "0",
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

// onMounted(() => {
//   getFunAllClassList();
// })
/** 查询学生信息列表 */
function getList() {
  loading.value = true;
  listStudentInfo(queryParams.value).then(response => {
    studentInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    stuCode: null,
    stuName: null,
    sex: null,
    age: null,
    brithdayTime: null,
    enterSchoolTime: null,
    graduateTime: null,
    status: null,
    studentClassId: null
  };
  proxy.resetForm("studentInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  names.value = selection.map(item => item.stuName);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学生信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStudentInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学生信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["studentInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStudentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStudentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 学籍状态修改  */
function handleStatusChange(row) {
  let text = row.status === "0" ? "在籍" : "不在籍";
  proxy.$modal.confirm('确认要将"' + row.stuName + '"同学设置为'+text+'吗?').then(function () {
    return changeStudentStatus(row.id,row.status);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "0" ? "1" : "0";
  });
};
/** 删除按钮操作 */
function handleDelete(row) {
  // const _ids = row.id || ids.value;
  const _Names = row.stuName || names.value;
  proxy.$modal.confirm('是否确认删除"' + _Names + '"学生信息项？').then(function() {
    return delStudentInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('student/studentInfo/export', {
    ...queryParams.value
  }, `studentInfo_${new Date().getTime()}.xlsx`)
}


/** 查询班级信息列表 */
function getFunAllClassList() {

  getAllClassList().then(response => {
    // console.log(response.data)
    classList.value = response.data;
  });
}

/** 根据班级Id，过滤查询出班级名称 */
function getClassNameById(classId){
  return classList.value.filter(item => item.id === classId)[0].className;
}

getFunAllClassList();
getList();

</script>
